package com.qianseit.westore.activity.account;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.google.zxing.client.android.FinishListener;
import com.qianseit.westore.AgentApplication;
import com.qianseit.westore.BaseDoFragment;
import com.qianseit.westore.Run;
import com.qianseit.westore.http.JsonRequestBean;
import com.qianseit.westore.http.JsonTask;
import com.qianseit.westore.http.JsonTaskHandler;
import com.beautymiracle.androidclient.R;

public class AccountNicknameFragment extends BaseDoFragment {

	private EditText mNewNameEdt;
	private JSONArray sttrs;

	public AccountNicknameFragment() {
		super();
	}

	@Override
	public void init(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActionBar.setTitle(R.string.account_nickname_change);
		mActionBar.setRightTitleButton(R.string.ok, new OnClickListener() {
			@Override
			public void onClick(View v) {
				new JsonTask().execute(new GetAccountNameTask());
			}
		});
		rootView = inflater.inflate(R.layout.fragment_account_nickname, null);
		mNewNameEdt = (EditText) findViewById(android.R.id.text1);
		mNewNameEdt.setText(AgentApplication.getApp(mActivity).getLoginedUser()
				.getNickName(mActivity));
	}

	private class GetAccountNameTask implements JsonTaskHandler {

		@Override
		public void task_response(String json_str) {
			try {
				// {"rsp":"succ","data":{"mem":"null","attr":[]},"res":""}
				JSONObject all = new JSONObject(json_str);
				if (Run.checkRequestJson(mActivity, all)) {
					JSONObject data = all.getJSONObject("data");
					if (data != null) { // null --> 没有可修改的属性
						sttrs = data.getJSONArray("attr");
						if (sttrs != null && sttrs.length() > 0) {
							// [{"attr_id":1,"attr_option":"","attr_show":"true","attr_column":"contact[name]","attr_required":"false","attr_value":null,"attr_type":"text","attr_tyname":"系统默认","attr_name":"姓名","attr_valtype":""}]
							for (int i = 0; i < sttrs.length(); i++) {
								JSONObject sttr = sttrs.optJSONObject(i);
								if (sttr.optString("attr_name").equals("姓名")) { // true
																				// 姓名属性可修改
									new JsonTask()
											.execute(new UpdateAccountNameTask());
									break;
								}
							}
						} else {
							hideLoadingDialog();
							Toast.makeText(mActivity, "商家暂时不支持修改昵称",
									Toast.LENGTH_SHORT).show();
						}
					}
				} else {

				}
			} catch (Exception e) {

			}
		}

		@Override
		public JsonRequestBean task_request() {
			showLoadingDialog();
			JsonRequestBean changeName = new JsonRequestBean(Run.API_URL,
					"mobileapi.member.setting");
			return changeName;
		}

	}

	private class UpdateAccountNameTask implements JsonTaskHandler {

		@Override
		public void task_response(String json_str) {
			hideLoadingDialog();
			Run.alertL(mActivity, "修改昵称成功");
			try {
				JSONObject all = new JSONObject(json_str);
				if (Run.checkRequestJson(mActivity, all)) {
					// 重新登录
					AgentApplication.getApp(mActivity).userAutoLogin();
					getActivity().finish();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		public JsonRequestBean task_request() {
			JsonRequestBean changeName = new JsonRequestBean(Run.API_URL,
					"mobileapi.member.save_setting");
			changeName.addParams("contact[name]", mNewNameEdt.getText()
					.toString());
			return changeName;
		}
	}
}
